package repository

import (
	"github.com/lab-online/internal/class/constant"
	"github.com/lab-online/internal/class/entity"
	"github.com/lab-online/pkg/database"
	"gorm.io/gorm"
)

type Repository struct {
	db *gorm.DB
}

var _ interface {
	DeleteClass(entity.ClassEntity) error
	SaveClass(entity.ClassEntity) (entity.ClassEntity, error)
	SaveWithStudents(entity.ClassEntity) (entity.ClassEntity, error)
	GetClassByID(uint) (entity.ClassEntity, error)
	GetClassWithStudentsByID(uint) (entity.ClassEntity, error)
	GetClasses(entity.ClassEntity) ([]entity.ClassEntity, error)
	GetClassIDsForStudent(username string) ([]uint, error)
	CheckClassIncludeStudent(class uint, student string) (bool, error)
} = &Repository{}

func New(postgres *gorm.DB) *Repository {
	return &Repository{postgres}
}

func errorHandler(err error) error {
	if database.IsRecordNotFound(err) {
		return constant.DBErrClassNotFound
	}
	return constant.DBErrUnknown
}
